if (!com) var com={};
if (!com.appspot) com.appspot={};
if (!com.appspot.twitterblogging) com.appspot.twitterblogging={};
com.appspot.twitterblogging={
	now:function(){
		return new Date().getTime();
	},
	getReleaseId:function() {
		rid = null;
		try {
			rid = window.ZemantaGetReleaseId();	
		} catch (er) {
			rid = 't' + Math.floor(new Date().getTime() / 10000000);
		}
		return rid;
	},
	load_file:function(file) {
		i = 0, j = 0, obj = null, head = null, release = '';
		if (file.constructor === Array) { // array
			for (i = 0, j = file.length; i < j; i += 1) {
				arguments.callee.call(this, file[i]);
			}
			return;
		}
		head = arguments.callee.head = arguments.callee.head || content.document.getElementsByTagName("head")[0];
		release = arguments.callee.release = arguments.callee.release || '?rel=' + this.getReleaseId();
		file += release;
		if (file.indexOf('.js') >= 0) { //if filename is a external JavaScript file
			obj = content.document.createElement('script');
			obj.setAttribute("type", "text/javascript");
			obj.setAttribute("src", file);
		} else if (file.indexOf('.css') >= 0) { //if filename is an external CSS file
			obj = content.document.createElement("link");
			obj.setAttribute("rel", "stylesheet");
			obj.setAttribute("type", "text/css");
			obj.setAttribute("href", file);
		} 
		if (head && obj) {
			head.appendChild(obj);
		}
	},
	loader:function(){
			alert('_before');
		   staticDomain = 'http://static.zemanta.com/devel/',
		   widget = document.createElement('div'),
		   insertionSpace = null,
			t0=this.now(),

		   widget.setAttribute('id', 'zemanta-sidebar');
		   widget.innerHTML = '<div id="zemanta-control" class="zemanta"></div><div id="zemanta-message" class="zemanta">Loading Zemanta...</div><div id="zemanta-filter" class="zemanta"></div><div id="zemanta-gallery" class="zemanta"></div><div id="zemanta-links" class="zemanta"></div><div id="zemanta-articles" class="zemanta"></div><!--<div id="zemanta-tags" class="zemanta"></div>--><div id="zemanta-preferences" class="zemanta"></div>';

			alert('_before');

			insertionSpace = content.document.getElementById('generalContent');
			alert('inserted_before');

			try{
				if (insertionSpace) {
					insertionSpace.parentNode.insertBefore(widget, insertionSpace);
					this.load_file([
							staticDomain + 'core/zemanta-widget.css',
							staticDomain + 'plugins/thunderbird/zemanta-widget-thunderbird.css',
							staticDomain + 'core/jquery.js',
							staticDomain + 'core/jquery.zemanta.js',
							staticDomain + 'plugins/thunderbird/zemanta-mail.js',
							staticDomain + 'plugins/thunderbird/thunderbird.js'
							]);
				} else if (this.now() - t0 < 5000) { // in 5s DOM should be available - should we check the ondomready/onload event?
					alert('settimeout');
					setTimeout(arguments.callee, 100);
				}
			} catch(e){
				alert(e);
			}
			alert('inserted');
	   }
}

